# -*- coding: utf-8 -*-

import  wx
import  wx.stc  as  stc

from  stc.stc_base import STCBase
import  depythonize

class STCIO(STCBase):
  def __init__(self, parent, ID, faces, stc_debug,
         pos=wx.DefaultPosition, size=wx.DefaultSize,
         style=0):
    STCBase.__init__(self, parent, ID, faces, pos, size, style)
    self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
    #self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
    #self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
    #self.Bind(stc.EVT_STC_MODIFIED, self.OnStcModified)
    #self.Bind(stc.EVT_STC_CHANGE, self.OnStcChange)
    #self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI_2)
    #self.stc_output = stc_output
    self.stc_debug = stc_debug

  #def OnStcChange(self, event):
  def OnUpdateUI_2(self, event):
    print event.GetModificationType(), ' GetModificationType'
    print event.GetText(), ' GetText'
    print event.GetKey(), ' GetKey'
    print self.GetCurrentPos(), ' GetCurrentPos'
    print self.GetSelectionStart(), ' GetSelectionStart'
    print self.GetSelectionEnd(), ' GetSelectionEnd'
    print event.GetY(), ' GetY'
    event.Skip()

  def OnKeyUp(self,event):
    text = self.GetText().encode('UTF-8')

    text_tokens = depythonize.translate_py(text)
    text_tokens = text_tokens.decode('UTF-8', 'replace')
    self.stc_debug.SetText(text_tokens)
    self.stc_debug.SetCurrentPos(self.GetCurrentPos())
    self.stc_debug.SetSelectionStart(self.GetSelectionStart())
    self.stc_debug.SetSelectionEnd(self.GetSelectionEnd())
    self.stc_debug.SetSTCFocus(True)

  def OnStcModified(self, event):
    pass
    #if event.GetModificationType() == 17: #ingreso un carácter
      #depythonize_buffer.add(event.GetText().encode('UTF-8'))
      #if (depythonize_does_modify)
        #GetCurrentPos
        #GetSelectionStart
        #SetText(depythonize.translate_py())
        #SetCurrentPos
        #SetSelectionStart
    #print event.GetKey(),'key'
    #print event.GetText(), 'Text'
    #print event.GetModificationType(), 'GetModificationType'

  def OnKeyPressed(self, event):
    #if self.CallTipActive():
      #self.CallTipCancel()
    #key = event.GetKeyCode()
    #self.python_stc_output.ExternalKeyPressed(self, event)
    #print dir(event)
    #print event.GetUniChar()
    #print event.GetUnicodeKey()
    #print event.GetKeyCode()

    if key == 32 and event.ControlDown():
      pos = self.GetCurrentPos()
    else:
      event.Skip()

